package com.chucang.shucang.ttp.listener;

import com.chucang.shucang.common.mq.constant.RabbitConstant;
import com.chucang.shucang.common.mq.to.ClosePayInfoTO;
import com.chucang.shucang.ttp.service.AliPayService;
import com.rabbitmq.client.Channel;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author flitsneak
 * @email flitsneak@gmail.com
 * @date 2022/10/27 20:16
 * @description 支付宝关单
 */
@Slf4j
@Component
@RequiredArgsConstructor
@RabbitListener(queues = RabbitConstant.ALIPAY_CLOSE_ORDER_QUEUE)
public class AliPayCloseOrderListener {

    private final AliPayService aliPayService;

    @RabbitHandler
    public void aliPayWithdrawListener(ClosePayInfoTO closePayInfoTO, Channel channel, Message message) throws IOException {
        log.info("订单{}，支付宝关单！", closePayInfoTO.getOrderSn());
        try {
            aliPayService.closeOrder(closePayInfoTO);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            log.error("关单失败！");
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }

}
